//
//  MBDPostCell.swift
//  MasterBoy
//
//  Created by kyson on 2023/10/22.
//

import Foundation
import YYText
import SnapKit

class MBDPostCell: MBDCommonCell {
    
    private var titleLabel : YYLabel!
    private var subtitleLabel : YYLabel!
    private var statusLabel : YYLabel!

    private var groupCoverImageView : UIImageView!
    
    private var separaterLine : UIView!
        
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.initialize()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        self.initialize()
    }
    
    func initialize() {
        self.contentView.setDynamicForgroundColor()
        self.commonBackgroundView.setDynamicForgroundColor()
        
        titleLabel = YYLabel()
        self.contentView.addSubview(titleLabel)
        
        statusLabel = YYLabel()
        self.contentView.addSubview(statusLabel)
        
        subtitleLabel = YYLabel()
        self.contentView.addSubview(subtitleLabel)
        
        separaterLine = UIView()
        self.contentView.addSubview(separaterLine)
        separaterLine.setDynamicSeperateLineColor()

        groupCoverImageView = UIImageView()
        groupCoverImageView.layer.cornerRadius = 4
        groupCoverImageView.clipsToBounds = true
        self.contentView.addSubview(groupCoverImageView)
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.top.equalTo(MBDMainCellModel.topMargin)
            make.width.equalTo(0)
            make.height.equalTo(0)
        }
        
        statusLabel.snp.makeConstraints { make in
            make.left.equalTo(titleLabel.snp.right).offset(8)
            make.centerY.equalTo(titleLabel)
            make.width.equalTo(0)
            make.height.equalTo(0)
        }
        
        subtitleLabel.snp.makeConstraints { make in
            make.left.equalTo(MBDMainCellModel.marginSubtitleLeft)
            make.top.equalTo(titleLabel.snp.bottom).offset(MBDPostCellModel.marginBetweenAvatarAndTitle)
            make.right.equalTo(-MBDMainCellModel.marginSubtitleRight)
            make.height.equalTo(0)
        }
        
        groupCoverImageView.snp.makeConstraints { make in
            make.left.equalTo(titleLabel)
            make.top.equalTo(subtitleLabel.snp.bottom).offset(MBDPostCellModel.groupCoverMarginTop)
            make.right.equalToSuperview().offset(-15)
            make.height.equalTo(0)
        }
        
        let scale = UIScreen.main.scale
        let h = ceil(0.5 * scale) / scale
        
        separaterLine.snp.makeConstraints { make in
            make.left.equalTo(groupCoverImageView)
            make.right.equalToSuperview()
            make.height.equalTo( h )
            make.bottom.equalToSuperview()
        }
    }
    
    deinit {
        print("MBDGroupMainCell was deallocated")
    }
    
    override var cellModel: Any! {
        didSet {
            let realCellModel = cellModel as! MBDPostCellModel
            
            self.titleLabel.textLayout = realCellModel.titlelayout
            self.statusLabel.textLayout = realCellModel.statusLayout

            titleLabel.snp.updateConstraints { make in
                make.height.equalTo(realCellModel.titlelayout!.textBoundingSize.height)
                make.width.equalTo(realCellModel.titlelayout!.textBoundingSize.width)
            }
            
            statusLabel.snp.updateConstraints { make in
                make.height.equalTo(realCellModel.statusLayout!.textBoundingSize.height)
                make.width.equalTo(realCellModel.statusLayout!.textBoundingSize.width)
            }
            
            self.subtitleLabel.textLayout = realCellModel.subtitlelayout
            
            subtitleLabel.snp.updateConstraints { make in
                make.height.equalTo(realCellModel.subtitlelayout!.textBoundingSize.height)
            }
            
            if let coverURL = realCellModel.groupCoverURL {
                
                let url = URL(string: coverURL)
                
                self.groupCoverImageView.kf.setImage(with: url)
                
                groupCoverImageView.snp.updateConstraints { make in
                    make.height.equalTo(MBDPostCellModel.groupCoverHeight)
                }
            } else {
                debugPrint("----")
            }
                        
        }
    }
}
